package com.pzl.simple.wvlib;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.pzl.simple.wvlib.bean.BigBean;

/**
 * 主进程处理远程调用实现
 * 主进程service接受webview进程js的调用和传参
 */
public class MainServiceImpl extends Service {

    IMainService.Stub impl = new IMainService.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
        }

        @Override
        public void handleJsCall(final String cmd, final String paramBeanJson) throws RemoteException {
            Log.i("jinx", "MainServiceImpl IMainService.Stub handleJsCall \n进程 = " + Utils.getCurProcessName(MainServiceImpl.this) + "\n"
                    + "线程 = " + Thread.currentThread().getName() + "\ncmd = " + cmd + "\nparamBeanJson = " + paramBeanJson);

            new Handler(getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainServiceImpl.this, "进程 = " + Utils.getCurProcessName(MainServiceImpl.this) + "\n"
                            + "\ncmd = " + cmd + "\nparamBeanJson = " + paramBeanJson, Toast.LENGTH_LONG).show();
                }
            });

            //根据cmd类型区分业务逻辑处理
            if ("doWorkA".equals(cmd)) {
                doWorkA(paramBeanJson);
            }
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return impl;
    }


    public void doWorkA(String msg) {
        Gson gson = new Gson();
        BigBean bigBean = gson.fromJson(msg, BigBean.class);
        Log.i("jinx", "doWorkA\n" + bigBean.toString());

    }


}
